package com.gemme.gemme.setting;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.gemme.gemme.BaseActivity;
import com.gemme.gemme.R;
import com.gemme.gemme.login.Constant;
import com.gemme.gemme.login.LoginActivity;
import com.gemme.gemme.manager.DataManager;
import com.gemme.gemme.view.Topview;
import com.gemme.gemme.view.TvTvArrow;
import com.gemme.gemme.wode.WoAccountActivity;
import com.jaeger.library.StatusBarUtil;

import java.util.HashMap;

public class SettingActivity extends BaseActivity implements View.OnClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        initView();
        initTop();
    }

    private void initTop() {
//        StatusBarUtil.setColor(this,getResources().getColor(R.color.white_color) );
        StatusBarUtil.setTransparent(this);
//        StatusBarUtil.setTranslucent(this,250);
        Topview topview = findViewById(R.id.topview);
        topview.setContentText("设置");
    }

    private void initView() {
      TvTvArrow grxx =  findViewById(R.id.grxx);
      TvTvArrow qchc =  findViewById(R.id.qchc);
      TvTvArrow sjh =  findViewById(R.id.sjh);
      TvTvArrow winxin =  findViewById(R.id.winxin);
      TvTvArrow szmm =  findViewById(R.id.szmm);
      TvTvArrow bzyfk =  findViewById(R.id.bzyfk);
      TvTvArrow gytbb =  findViewById(R.id.gytbb);

        grxx.setLeftText("我的账号").setRightText("").setRightVisble(true);
        qchc.setLeftText("清除缓存").setRightText("0.00M").setRightVisble(true);
        sjh.setLeftText("手机号").setRightText("13515600327").setRightVisble(true);
        winxin.setLeftText("微信").setRightText("已绑定").setRightVisble(true);
        szmm.setLeftText("设置密码").setRightText("未设置").setRightVisble(true);
        bzyfk.setLeftText("意见反馈").setRightText("").setRightVisble(true);
        gytbb.setLeftText("用户协议").setRightText("").setRightVisble(true);
        
        SharedPreferences MSP = getSharedPreferences(Constant.MSP, Context.MODE_PRIVATE);
        MSP.getString(Constant.ACCESS_TOKEN, "").trim();
        findViewById(R.id.exit_login).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                loginOut();

            }
        });
        grxx.setOnClickListener(this);
        gytbb.setOnClickListener(this);
        bzyfk.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.gytbb:
                startActivity(new Intent(mContext,XieyiActivity.class));
                break;
            case R.id.bzyfk:
                startActivity(new Intent(mContext,AdviceActivity.class));
                break;

                case R.id.grxx:
                startActivity(new Intent(mContext, WoAccountActivity.class));
                break;
        }
    }

    private void loginOut() {
        //http://192.168.1.6:9999/auth/token/applogout
        String url = DataManager.BaseUrl + "auth/token/applogout";
        HashMap<String, String> headHashMap = new HashMap<>();
        SharedPreferences MSP = getSharedPreferences(Constant.MSP, Context.MODE_PRIVATE);
        String ACCESS_TOKEN = MSP.getString(Constant.ACCESS_TOKEN, "").trim();
        if(TextUtils.isEmpty(ACCESS_TOKEN))
            return;
        headHashMap.put(DataManager.AuthorizationKey,"Bearer " + ACCESS_TOKEN);
        DataManager.toServer2(mContext, url, DataManager.GET_TYPE, "", headHashMap, new DataManager.GeResponse(){

            @Override
            public void onResponse(String response) {

                try {
                    System.out.println("");
                    cleardata();
                    startActivity(new Intent(mContext, LoginActivity.class));
                    finish();
                }catch (Exception e){

                }
            }
        });
    }

    private void cleardata() {
        SharedPreferences MSP = getSharedPreferences(Constant.MSP, Context.MODE_PRIVATE);
        MSP.edit().putString(Constant.ACCESS_TOKEN, "").apply();
        MSP.edit().putString(Constant.REFRESH_TOKEN, "").apply();
        MSP.edit().putString(Constant.ACCESS_TOKENTIME, "").apply();
    }


}